package indi.yuluo.exception;

import indi.yuluo.vo.Msg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: yuluo
 * @CreateTime: 2022-07-21  11:37
 * @Description: 全局异常处理
 */

@ControllerAdvice(annotations = {RestController.class, Controller.class})   // 指定拦截的控制器
@Slf4j
@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 处理登录时产生的异常
     * @param loginException 用户登录异常
     * @return
     */
    @ExceptionHandler(LoginException.class)
    public Msg exceptionHandler(LoginException loginException) {

        log.error(loginException.getMessage());

        return Msg.fail().add("loginException", loginException.getMessage());
    }

}
